package org.dromara.app.config;

import io.rong.RongCloud;
import io.rong.models.CheckMethod;
import io.rong.models.Result;
import io.rong.models.group.GroupMember;
import io.rong.models.group.GroupMemberCount;
import io.rong.models.group.GroupModel;
import io.rong.models.message.GroupMessage;
import io.rong.models.response.ResponseResult;
import io.rong.util.CommonUtil;
import io.rong.util.GsonUtil;
import io.rong.util.HttpUtil;
import jakarta.validation.Valid;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.net.HttpURLConnection;
import java.net.URLEncoder;



@Service
public class AppRongYunUtil {


    private static final String UTF8 = "UTF-8";
    private static final String PATH = "group";
    @Value("${rongyun.app-key}")
    private String appKey;
    @Value("${rongyun.app-secret}")
    private String appSecret;



    /**
     * 创建群组方法（创建群组，并将用户加入该群组，用户将可以收到该群的消息，同一用户最多可加入 500 个群，每个群最大至 3000 人，App 内的群组数量没有限制.注：其实本方法是加入群组方法 /group/join 的别名。）
     *
     * url /group/create.json
     * docs https://doc.rongcloud.cn/imserver/server/v1/im-server-api-list-v1
     *
     * @param group 创建群组的群组信息
     *
     * @return Result
     **/
    public Result create(GroupModel group) throws Exception {
        RongCloud rongCloud = RongCloud.getInstance(appKey, appSecret);
        Result groupCreateResult = (Result)rongCloud.group.create(group);
        return groupCreateResult;
    }


    /**
     * 将用户加入指定群组，用户将可以收到该群的消息，同一用户最多可加入 500 个群，每个群最大至 3000 人。
     *
     * @param group 用户加入指定群组参数
     *
     * @return Result
     **/
    public Result join(GroupModel group) throws Exception {
        RongCloud rongCloud = RongCloud.getInstance(appKey, appSecret);
        Result groupJoinResult = (Result)rongCloud.group.join(group);
        return groupJoinResult;
    }

    /**
     * 退出
     */
    public Result quit(GroupModel group) throws Exception {
        RongCloud rongCloud = RongCloud.getInstance(appKey, appSecret);
        Result groupQuitResult = (Result)rongCloud.group.quit(group);
        System.out.println("group create result:  " + groupQuitResult.toString());
        return groupQuitResult;
    }
    /**
     * 解散
     */
    public Result dismiss(GroupModel group) throws Exception {
        RongCloud rongCloud = RongCloud.getInstance(appKey, appSecret);
        Result groupDismissResult = (Result)rongCloud.group.dismiss(group);
        System.out.println("group create result:  " + groupDismissResult.toString());
        return groupDismissResult;
    }

    /**
     * 发送群聊消息
     */
    public Result publish(GroupMessage message) throws Exception {
        RongCloud rongCloud = RongCloud.getInstance(appKey, appSecret);
        Result groupDismissResult = (Result)rongCloud.message.group.send(message);
        System.out.println("group create result:  " + groupDismissResult.toString());
        return groupDismissResult;
    }

    /**
     * 查询群组成员
     */
    public Result query(GroupModel group) throws Exception {
        RongCloud rongCloud = RongCloud.getInstance(appKey, appSecret);
        Result groupDismissResult = (Result)rongCloud.group.get(group);
        System.out.println("group create query:  " + groupDismissResult.toString());
        return groupDismissResult;
    }

}
